package com.hellowx.qydcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hellowx.qydcdev.constant.RedisPrefix;
import com.hellowx.qydcdev.dao.CustomerDao;
import com.hellowx.qydcdev.entity.Customer;
import com.hellowx.qydcdev.entity.LoginVO;
import com.hellowx.qydcdev.exception.CustomException;
import com.hellowx.qydcdev.exception.CustomExceptionType;
import com.hellowx.qydcdev.service.CustomerService;
import com.hellowx.qydcdev.utils.ImageUtils;
import com.hellowx.qydcdev.utils.MD5Utils;
import com.hellowx.qydcdev.utils.RedisUtils;
import com.hellowx.qydcdev.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created on 2022/1/5.
 *
 * @author Hello WX
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private CustomerDao customerDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean register(LoginVO loginVO, HttpServletRequest request) {
        String ipAddr = UserUtils.getIpAddr(request);
        String captcha = (String) redisUtils.get(ipAddr + RedisPrefix.CAPTCHA);
        redisUtils.del(ipAddr+RedisPrefix.CAPTCHA);
        if (captcha==null || !captcha.equals(loginVO.getCheckCode())){
            throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"验证码已过期或验证码输入错误!");
        }
        if (!loginVO.getPassword().equals(loginVO.getCheckPassword())){
            throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"两次输入的密码不一样!!");
        }
        Customer customer = new Customer();
        customer.setName(loginVO.getName());
        customer.setAvatar(ImageUtils.randomAvatar());
        customer.setNickName(loginVO.getName());
        customer.setPhone(loginVO.getPhone());
        customer.setPassword(MD5Utils.generatePwd(loginVO.getPassword()));
        LambdaQueryWrapper<Customer> lq = Wrappers.lambdaQuery();
        lq.eq(Customer::getPhone,loginVO.getPhone());
        Customer customerPO = customerDao.selectOne(lq);
        if (customerPO!=null){
            throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"该手机号码已经注册过了");
        }
        int insert = customerDao.insert(customer);
        return insert>0;
    }

    @Override
    public String login(LoginVO loginVO) {
        LambdaQueryWrapper<Customer> lq = Wrappers.lambdaQuery();
        lq.eq(Customer::getPhone,loginVO.getPhone());
        Customer customerPO = customerDao.selectOne(lq);
        if (customerPO==null){
            throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"该手机号未注册");
        }
        boolean verifyPwd = MD5Utils.verifyPwd(loginVO.getPassword(), customerPO.getPassword());
        if (!verifyPwd){
            throw new CustomException(CustomExceptionType.USER_INPUT_EXCEPTION,"密码错误");
        }
        UUID uuid = UUID.randomUUID();
        boolean result =  redisUtils.set(uuid.toString(), customerPO,604800L);
        if (result){
            return uuid.toString();
        }
        throw new CustomException(CustomExceptionType.SYSTEM_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateCustomer(Customer customer, HttpServletRequest request) {
        if (customer.getPassword()!=null){
            customer.setPassword(MD5Utils.generatePwd(customer.getPassword()));
        }
        int rows = customerDao.updateById(customer);
        Customer one = new LambdaQueryChainWrapper<>(customerDao).eq(Customer::getId, customer.getId()).one();
        String token = request.getHeader("Token");
        boolean result =  redisUtils.set(token, one,604800L);
        if (result && rows>0){
            return "更新成功";
        }
        return "更新失败";
    }
}
